//
//  UserInfo.swift
//  SCA
//
//  Created by tjun on 2025/8/31.
//

import Foundation

final class UserInfo {
    
    private enum Key {
        static let username = "user_username"
        static let token = "user_token"
        static let userId = "user_Id"
        static let isLogin = "user_isLogin"
        static let avatar = "user_avatar"
        static let nickname = "user_nickname"
    }
    
    private static let defaults = UserDefaults.standard

    // MARK: - 属性方法
    static var token: String? {
        get { defaults.string(forKey: Key.token) }
        set { defaults.set(newValue, forKey: Key.token) }
    }
    
    static var username: String {
        get { defaults.string(forKey: Key.username) ?? "" }
        set { defaults.set(newValue, forKey: Key.username) }
    }
    
    static var userId: Int {
        get { defaults.integer(forKey: Key.userId) }
        set { defaults.set(newValue, forKey: Key.userId) }
    }
    
    static var isLogin: Bool {
        get { defaults.bool(forKey: Key.isLogin) }
        set {
            defaults.set(newValue, forKey: Key.isLogin)
            if isLogin == false {
                defaults.removeObject(forKey: Key.token)
            }
        }
    }
    
    static var avatar: String? {
        get { defaults.string(forKey: Key.avatar) }
        set { defaults.set(newValue, forKey: Key.avatar) }
    }
    
    static var nickname: String? {
        get { defaults.string(forKey: Key.nickname) }
        set { defaults.set(newValue, forKey: Key.nickname) }
    }

    static func removeToken() {
        defaults.removeObject(forKey: Key.token)
    }
        
    // MARK: - 清空
    static func clear() {
        defaults.removeObject(forKey: Key.isLogin)
        defaults.removeObject(forKey: Key.nickname)
        defaults.removeObject(forKey: Key.avatar)
        defaults.removeObject(forKey: Key.userId)
    }
    
}

